﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

using System;
using SlimDX.Multimedia;
using Tesla.Input;
using RI = SlimDX.RawInput;
using SWF = System.Windows.Forms;

namespace Tesla.Windows.Input {
    /// <summary>
    /// Keyboard wrapper using Windows RawInput.
    /// </summary>
    public sealed class RawInputKeyboardWrapper : IKeyboardWrapper {
        private static uint _maskValue = uint.MaxValue;

        private uint _keyState0;
        private uint _keyState1;
        private uint _keyState2;
        private uint _keyState3;
        private uint _keyState4;
        private uint _keyState5;
        private uint _keyState6;
        private uint _keyState7;

        /// <summary>
        /// Initializes a new instance of the <see cref="RawInputKeyboardWrapper"/> class.
        /// </summary>
        public RawInputKeyboardWrapper() {
            RI.Device.RegisterDevice(UsagePage.Generic, UsageId.Keyboard, RI.DeviceFlags.None);
            RI.Device.KeyboardInput += new EventHandler<RI.KeyboardInputEventArgs>(KeyboardInput);
        }

        /// <summary>
        /// Gets the current state of the keyboard - e.g. which keys are pressed.
        /// </summary>
        /// <returns>
        /// The current keyboard state.
        /// </returns>
        public KeyboardState GetKeyboardState() {
            return new KeyboardState(_keyState0, _keyState1, _keyState2, _keyState3, _keyState4, _keyState5, _keyState6, _keyState7);
        }

        private void KeyboardInput(Object sender, RI.KeyboardInputEventArgs e) {
            SWF.Keys key = e.Key;
            RI.KeyState state = e.State;

            if(state == RI.KeyState.Pressed) {
                AddPressedKey((int) key);
            } else if(state == RI.KeyState.Released) {
                RemovePressedKey((int) key);
            }
        }

        private void AddPressedKey(int key) {
            //Get the bit place in the state for the key
            uint place = ((uint) 1) << key;
            //Get the state grouping for the key
            int grouping = key >> 5;
            //Add the key to the proper state
            switch(grouping) {
                case 0:
                    _keyState0 |= place & _maskValue;
                    return;
                case 1:
                    _keyState1 |= place & _maskValue;
                    return;
                case 2:
                    _keyState2 |= place & _maskValue;
                    return;
                case 3:
                    _keyState3 |= place & _maskValue;
                    return;
                case 4:
                    _keyState4 |= place & _maskValue;
                    return;
                case 5:
                    _keyState5 |= place & _maskValue;
                    return;
                case 6:
                    _keyState6 |= place & _maskValue;
                    return;
                case 7:
                    _keyState7 |= place & _maskValue;
                    return;
            }
        }

        private void RemovePressedKey(int key) {
            //Get the bit place for the key
            uint place = ((uint) 1) << (int) key;
            //Get the state grouping for the key
            int grouping = key >> 5;
            //Remove the key from the proper state
            switch(grouping) {
                case 0:
                    _keyState0 &= ~(place & _maskValue);
                    return;
                case 1:
                    _keyState1 &= ~(place & _maskValue);
                    return;
                case 2:
                    _keyState2 &= ~(place & _maskValue);
                    return;
                case 3:
                    _keyState3 &= ~(place & _maskValue);
                    return;
                case 4:
                    _keyState4 &= ~(place & _maskValue);
                    return;
                case 5:
                    _keyState5 &= ~(place & _maskValue);
                    return;
                case 6:
                    _keyState6 &= ~(place & _maskValue);
                    return;
                case 7:
                    _keyState7 &= ~(place & _maskValue);
                    return;
            }
        }
    }
}
